package com.antler.smt.admin.controller;

import com.antler.smt.admin.annotation.OperateCut;
import com.antler.smt.constant.PermissionConstant;
import com.antler.smt.entity.RequirementEntity;
import com.antler.smt.entity.SupplierEntity;
import com.antler.smt.entity.UserEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.EditRequirementAddressReq;
import com.antler.smt.request.admin.GetRequirementListReq;
import com.antler.smt.request.admin.GetSupplierListReq;
import com.antler.smt.request.admin.JoinSupplierReq;
import com.antler.smt.response.admin.GetRequirementListResp;
import com.antler.smt.response.admin.GetSupplierListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.OrderService;
import com.antler.smt.service.admin.PurchaserService;
import com.antler.smt.service.admin.RequirementService;
import com.antler.smt.service.admin.SupplierService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

/**
 * 发布需求Controller
 *
 * @author zhangyi
 * @date 2018/3/23 14:17
 */
@RestController
@RequestMapping("/requirementBack")
@Api(description = "后台发布需求入口")
public class RequirementController extends BaseController{

    /**
     * 日志
     */
    private static Logger logger = LogUtil.get();

    @Resource
    private RequirementService requirementService;

    @Resource
    private PurchaserService purchaserService;

    @Resource
    private SupplierService supplierService;

    @Resource
    private OrderService orderService;

    /**
     * 获取采购商的发布需求列表
     *
     * @return
     */
    @PostMapping("/listPurchaserRequirement")
    @ApiOperation(value = "采购商的发布需求列表", httpMethod = "POST")
    public Result<GetRequirementListResp> listPurchaserRequirement(@RequestBody GetRequirementListReq request){
        DataValidator.isNull(request,"发布需求请求不能为空！");
        DataValidator.isBlank(String.valueOf(request.getPageCurrent()),"分页不能为空！");
        DataValidator.isBlank(String.valueOf(request.getPageSize()),"分页大小不能为空！");

        Result result = null;
           result = purchaserService.getPurchaserRequirementList(request);
        return result;
    }

    /**
     * 获取发布需求详细信息
     *
     * @param requirementId 获取发布需求id
     * @return 列表实体
     */
    @PostMapping("/requirementInfo/{requirementId}")
    @ApiOperation(value = "获取发布需求详细列表", httpMethod = "POST")
    public Result<GetRequirementListResp> requirementInfo(@PathVariable long requirementId){
        DataValidator.isBlank(String.valueOf(requirementId),"发布需求id不能为空！");
        Result result = null;
            result = requirementService.requirementInfo(requirementId);
        return result;
    }

    /**
     *需求驳回
     *
     * @return
     */
    @PostMapping("/requirementFail/{requirementId}")
    @ApiOperation(value = "需求驳回", httpMethod = "POST")
    @OperateCut(content = "发布需求驳回")
    public Result<ResultCodeEnum> requirementFail(@PathVariable Long requirementId){

        DataValidator.isBlank(String.valueOf(requirementId),"发布需求id不能为空！");

        Result result = null;
            result = requirementService.requirementFail(requirementId);
        return result;
    }

    /**
     *  需求修改
     *
     * @param request
     * @return
     */
    @PostMapping("/requirementUpdate")
    @ApiOperation(value = "需求修改", httpMethod = "POST")
    @OperateCut(content = "修改其发布需求信息")
    public Result<ResultCodeEnum> requirementUpdate(@RequestBody RequirementEntity request){

        DataValidator.isNull(request,"需求请求实体不为空！");
        DataValidator.isBlank(String.valueOf(request.getId()),"需求id不为空！");

        Result result = null;
            result = requirementService.requirementUpdate(request);
        return result;
    }

    /**
     *  需求通过
     *
     * @return
     */
    @PostMapping("/requirementPass/{requirementId}")
    @ApiOperation(value = "需求通过，更新订单字段", httpMethod = "POST")
    @OperateCut(content = "发布需求通过")
    public Result<ResultCodeEnum> requirementPass(@PathVariable Long requirementId){
        Result result = null;
            result = requirementService.requirementPass(requirementId);
        return result;
    }

    /**
     * 项目专员选择适合的服务商
     *
     * @param request 服务商实体
     */
    @PostMapping("/findSuitableSupplier")
    @ApiOperation(value = "项目专员选择适合的服务商", httpMethod = "POST")
    @OperateCut(content = "对接其服务商")
    public Result<GetSupplierListResp> findSuitableSupplier(@RequestBody GetSupplierListReq request){

        DataValidator.isNull(request,"服务商实体不能为空！");

        Result result = null;
            result = supplierService.getSupplierList(request);
        return result;
    }

    /**
     *  商务专员填写对采购商的价格
     *
     * @param orderId
     * @param price
     * @return
     */
    @PostMapping("/writePurchaserPrice")
    @ApiOperation(value = "商务专员填写对采购商的价格", httpMethod = "POST")
    @OperateCut(content = "商务专员填写对采购商的价格")
    public Result<ResultCodeEnum> writePurchaserPrice(@RequestParam(value = "orderId") Long orderId,
                                                      @RequestParam(value = "price") Long price){
        Result result = null;
            result = orderService.updatePurchaserPrice(orderId,price);
        return result;
    }

    /**
     *  对接服务商和采购商(更新订单字段)
     *
     * @param req
     * @return
     */
    @PostMapping("/joinSupplierAndPurchaser")
    @ApiOperation(value = "对接服务商和采购商", httpMethod = "POST")
    @OperateCut(content = "对接服务商和采购商")
    public Result<ResultCodeEnum> joinSupplierAndPurchaser(@RequestBody JoinSupplierReq req){
        Result result = null;
            result = orderService.updateOrderSupplierId(req);
        return result;
    }

    /**
     *  项目专员确认采购商的付款金额
     *
     * @param orderId 订单id
     * @param payAuditType 申请支付类型
     * @param payAuditAmmont 申请支付金额
     * @param purchaserId 服务商id
     * @return
     */
    @PostMapping("/auditPayment/{orderId}")
    @ApiOperation(value = "项目专员确认采购商的付款金额", httpMethod = "POST")
    @OperateCut(content = "项目专员确认其采购商的付款金额")
    public Result<ResultCodeEnum> auditPayment(@PathVariable Long orderId,
                                               @RequestParam(value = "payAuditType") Integer payAuditType,
                                               @RequestParam(value = "payAuditAmmont") Long payAuditAmmont,
                                               @RequestParam(value = "purchaserId") Long purchaserId){

        DataValidator.isNull(String.valueOf(orderId),"订单id不能为空！");
        DataValidator.isBlank(String.valueOf(payAuditType),"支付类型不能为空！");
        DataValidator.isBlank(String.valueOf(payAuditAmmont),"审核支付金额不能为空！");
        DataValidator.isBlank(String.valueOf(purchaserId),"采购商id不能为空");

        Result result = null;
            result = orderService.auditPayment(orderId,payAuditType,payAuditAmmont,purchaserId);
        return result;
    }

    /**
     *  项目专员付款给服务商
     *
     * @return
     */
    @PostMapping("/paymentSupplier/{orderId}")
    @ApiOperation(value = "项目专员付款给服务商", httpMethod = "POST")
    @OperateCut(content = "项目专员付款给服务商")
    public Result<ResultCodeEnum> paymentSupplier(@PathVariable Long orderId){

        DataValidator.isNull(String.valueOf(orderId),"订单id不能为空！");

        Result result = null;
            Long userId = getUserId();

            if(PermissionConstant.SUPER_ADMIN.equals(userId)){
                result = orderService.paymentSupplier(orderId);
            }else{
                result = new Result(ResultCodeEnum.MODIFY_DATA_ERROR,"您不是超级管理员，无法付款给服务商");
            }
        return result;
    }

    /**
     *  修改发布需求的收货地址（对服务商展示）
     *
     * @return
     */
    @PostMapping("/editRequirementAddress")
    @ApiOperation(value = "修改发布需求的收货地址（对服务商展示）", httpMethod = "POST")
    @OperateCut(content = "修改对采购商的发货地址")
    public Result<ResultCodeEnum> editRequirementAddress(@RequestBody EditRequirementAddressReq req){

        DataValidator.isNull(req,"发布实体不能为空！");
        DataValidator.isBlank(String.valueOf(req.getRequirementId()),"发布需求id不能为空！");

        Result result = null;
            result = requirementService.editRequirementAddress(req);
        return result;
    }

}


